<%@page contentType="text/html; charset=utf-8"%>
<%@ include file="../inc/TagLibs.jsp"%>
<c:set var="pageTitle" value="新增附件" />
<%@ include file="../inc/Title.jsp"%>
<head>
<link rel="stylesheet" type="text/css" href="/css/base.css" />
<script src="/js/popup_ext.js"></script>
<script src="/js/jquery.js"></script>
<script src="/js/EkUpload.js"></script>
<script language="javascript">
	var insertOk=false;
	var insertCount=0;
	function doWinReturn(){
		if(insertOk){
			parent.popRetVal="OK";
		}else{
			parent.popRetVal=null;
		}
		parent.ClosePop();
	}
	function checkReturnValue(){
		if(insertOk){
			parent.popRetVal="OK";
		}else{
			parent.popRetVal=null;
		}
	}
	
	function doStart(){
		document.getElementById("startButton").disabled=true;
		document.getElementById("pauseButton").disabled=false;
		
		var customPath=document.getElementById("customPath").value.trim();
		var customMark=document.getElementById("customMark").value.trim();
		var fileArr=new Array();
		for(var i=1;i<6;i++){
			var files=document.getElementById("fileName"+i).files;
			var wcId=document.getElementById("extId"+i).value;
			var accType;
			if(document.getElementById("accType"+i+"_1").checked){
				accType="DESIGN";
			}else if(document.getElementById("accType"+i+"_2").checked){
				accType="COOPER";
			}else{
				accType="CONTRACT";
			}
			
			if(files.length!=0){
				var lowerFn=files[0].name.toLowerCase();
				if(lowerFn.endWith(".exe") || lowerFn.endWith(".bat") || lowerFn.endWith(".com") || lowerFn.endWith(".jsp") || lowerFn.endWith(".htm") || lowerFn.endWith(".html")){
					document.getElementById("startButton").disabled=false;
					document.getElementById("pauseButton").disabled=true;
					ShowAlert('提示框','不能上传.exe、.bat、.com、.jsp、.htm或.html格式的文件！',200,100);
					return;
				}else if(accType=="DESIGN" && (wcId=="" || wcId=="0")){
					document.getElementById("startButton").disabled=false;
					document.getElementById("pauseButton").disabled=true;
					ShowAlert('提示框','请选择工作中心！',200,100);
					return;
				}
				fileArr.push(files[0]);
			}
		}
		
		if(fileArr.length>0){
			//不给修改,否则upload后插入数据有可能有偏差
			for(var i=1;i<6;i++){
				document.getElementById("fileName"+i).disabled=true;
				document.getElementById("extId"+i).disabled=true;
				document.getElementById("accType"+i+"_1").disabled=true;
				document.getElementById("accType"+i+"_2").disabled=true;
				document.getElementById("accType"+i+"_3").disabled=true;
				document.getElementById("mark"+i).disabled=true;
			}
			EkUpload.upload(
				fileArr,
				function(msg){
					ShowAlert('提示框',msg,200,100);
				},
				showUploadName,
				showUploadProc,
				doUploadCallback
			);
		}else if(customPath!=""){
			var wcId=document.getElementById("extId").value;
			var accType;
			if(document.getElementById("accType_1").checked){
				accType="DESIGN";
			}else if(document.getElementById("accType_2").checked){
				accType="COOPER";
			}else{
				accType="CONTRACT";
			}
			if(accType=="DESIGN" && (wcId=="" || wcId=="0")){
				document.getElementById("startButton").disabled=false;
				document.getElementById("pauseButton").disabled=true;
				ShowAlert('提示框','请选择工作中心！',200,100);
				return;
			}else{
				doInsertRecord(customPath,"",customMark,wcId,accType,"PATH");
			}
		}else{
			document.getElementById("startButton").disabled=false;
			document.getElementById("pauseButton").disabled=true;
			ShowAlert('提示框','请选择要上传的文件，或输入资料路径！',200,100);
			return;
		}
	}
	function showUploadName(fileName){
		$("#curFile").text(fileName);
	}
	function showUploadProc(total,ci){
		var totalStr=parseFileSize(total);
		var ciStr=parseFileSize(ci);
		var percentStr;
		if(total!=0){
			percentStr=(Math.round(ci*100/total)).toString()+"%";
			if(percentStr!="0%"){
				document.getElementById("proTd").style.width=percentStr;
				document.getElementById("percentTd").innerHTML=percentStr;
				if(ci==total){//100%
					document.getElementById("remainTd").style.display="none";
				}else{
					document.getElementById("remainTd").style.display="";
				}
			}
		}
		document.getElementById("proSpan").innerHTML="已传"+ciStr+"，总文件大小"+totalStr;
	}
	function doUploadCallback(fileUrl,index){
		var fileIndex=0;
		for(var i=1;i<6;i++){
			var files=document.getElementById("fileName"+i).files;
			if(files.length!=0){
				if(index==fileIndex){
					var mark=document.getElementById("mark"+i).value;
					var extId=document.getElementById("extId"+i).value;
					var accType;
					if(document.getElementById("accType"+i+"_1").checked){
						accType="DESIGN";
					}else if(document.getElementById("accType"+i+"_2").checked){
						accType="COOPER";
					}else{
						accType="CONTRACT";
					}
					doInsertRecord("",fileUrl,mark,extId,accType,"FILE");
					return;
				}else{
					fileIndex++;
				}
			}
		}
	}
	function doPause(){
		if(document.getElementById("pause_flag").value==""){
			EkUpload.pause();
			document.getElementById("pause_flag").value="PAUSE";
			document.getElementById("pauseButton").value="继续";
		}else{
			EkUpload.restart();
			document.getElementById("pause_flag").value="";
			document.getElementById("pauseButton").value="暂停";
		}
	}
	
	function doInsertRecord(customPath,fileName,mark,extId,accType,uploadType){
		fileName=encodeURIComponent(fileName);
		var customPath=encodeURIComponent(customPath);
		var agentId="${agentId}";
		var agentType="${agentType}";
		var code="${code}";
		var mark=encodeURIComponent(mark);
		var url="/business/AccessoryInsertRecord.ihtm?return_type=AJAX&agent_id="+agentId+"&agent_type="+agentType+"&code="+code+"&file_name="+fileName+"&mark="+mark+"&custom_path="+customPath+"&ext_id="+extId+"&acc_type="+accType;
		var xmlHttp=createXMLHttpRequest();
		xmlHttp.open("GET", url, false);
		xmlHttp.onreadystatechange = function(){
			if (xmlHttp.readyState == 4) {
				if (xmlHttp.status == 200) {
					var itemId=xmlHttp.responseText;
	      			if(itemId!="0"){
						insertOk=true;//用于没全部传完就关闭窗口
						insertCount++;
						
						if(uploadType=="FILE"){//第二种形式
							var fileCount=0;
							for(var i=1;i<6;i++){
								var files=document.getElementById("fileName"+i).files;
								if(files.length!=0){
									fileCount++;
								}
							}
							if(insertCount==fileCount){
								//全部完成
								doWinReturn();
							}
						}else{//第一种形式
							doWinReturn();
						}
						
	      			}
				}
			}
		};
		xmlHttp.send(null);
	}
	function showMe(){
		if(checkNavigator()[0]!="msie"){
			for(var i=1;i<6;i++){
				document.getElementById("fileName"+i).className="";
			}
		}
	}
	function keepAlive(){
		var currentTime=new Date();
		var xmlHttp=createXMLHttpRequest();
		xmlHttp.open("GET", "/Foot.ihtm?random="+currentTime.getTime(),true);
		xmlHttp.send(null);
		setTimeout("keepAlive()",1000*60*10);
	}
	function selectAccMark(objId){
		var url="/base/AccessoryMarkListForSelect.ihtm";
		ShowIframe("选择文件说明",url,500,400,"selectAccMarkCallBack('"+objId+"')");
	}
	function selectAccMarkCallBack(objId){
		if(popRetVal!=null && popRetVal!=""){
			document.getElementById(objId).value=popRetVal;
		}
	}
	
	function browseFolder(){
		try{
			var shell = new ActiveXObject("Shell.Application");
			var folder = shell.BrowseForFolder(0, "请选择目录", 64, 17);//起始目录为：我的电脑
			//var folder = shell.BrowseForFolder(0,"请选择目录",0); //起始目录为：桌面
			if(folder!=null){
				folder = folder.items(); // 返回 FolderItems 对象
            	folder = folder.item(); // 返回 Folderitem 对象
            	folder = folder.Path;   // 返回路径
            	if (folder.charAt(folder.length - 1) != "\\") {
                	folder = folder + "\\";
            	}
            	document.getElementById("customPath").value = folder;
			}
		}catch(e){
			ShowAlert('提示框',e.message,200,100);
		}
	}
	function changePathAccType(){
		if(!document.getElementById("accType_1").checked){
			document.getElementById("extId").options[0].selected=true;
		}
	}
	function changeFileAccType(index){
		if(!document.getElementById("accType"+index+"_1").checked){
			document.getElementById("extId"+index).options[0].selected=true;
		}
	}
	function doWcChange(index){
		var si=document.getElementById("extId"+index).selectedIndex;
		for(var i=index;i<=5;i++){
			document.getElementById("extId"+i).selectedIndex=si;
		}
	}
</script>
</head>
<body scroll="no" onLoad="showMe();keepAlive();" onUnload="checkReturnValue();" class="sheet modal-dialog" style="padding-top:10px;">
<div class="sheet-main" style="width:830px;" id="bodyDiv">
  <div class="sheet-list" style="width:100%;">
    <div class="modal-main search-business">
    	<div style="width:100%;">
 			<c:set var="pageTopTitle" value="新增附件" />
			<%@ include file="../inc/PageTop.jsp"%>
			<table border="0" cellspacing="1" cellpadding="1" align="center" class="table-a" >
			</table>
			<fieldset class="det_fieldset ">
				<legend class="mag10">第一种方式：不传文件，只登记文件存储路径</legend>
				<table border="0" cellspacing="1" cellpadding="1" class="table-0" style="margin-left:4px">
					<tr>
			      		<th style="width:10%;height:26px">存储路径：</th>
				      	<td style="width:26%">
				      		<input type="text" style="width:150px" maxlength="100" name="customPath" id="customPath" class="TextBox">
				      		<input type="button" value="..." onClick="browseFolder();">
				     	</td>
				      	<th style="width:10%">文件说明：</th>
				      	<td style="width:54%">
				      		<input type="text" maxlength="50" name="customMark" id="customMark" class="TextBox">
				      		<img class="make-a" src="/images/make-a.gif" onClick="selectAccMark('customMark');" style="cursor:pointer;" />
				      		<select name='extId' id='extId' style="width:80px">
				      			<option value="0">==请选择==</option>
								<c:forEach var="wcItem" items="${wcList}" varStatus="extIdIndex">
									<option value="${wcItem.id}" <c:if test="${accType!='COOPER' && (extId==0 && extIdIndex.index==0 || extId!=0 && wcItem.id==extId)}">selected</c:if>>${wcItem.wcName}</option>
								</c:forEach>
				  			</select>
							<input type="radio" name="accType" id="accType_1" <c:if test="${accType!='COOPER'}">checked="true"</c:if> onClick="changePathAccType();">自制
							<input type="radio" name="accType" id="accType_2" <c:if test="${accType=='COOPER'}">checked="true"</c:if> onClick="changePathAccType();">外协
							<input type="radio" name="accType" id="accType_3" onClick="changePathAccType();">合同
				     	</td>
					</tr>
				</table>
			</fieldset>
			
			<div class="blank12"></div> 
			<fieldset class="det_fieldset ">
				<legend class="mag10">第二种方式：上传到服务器</legend>
				<table border="0" cellspacing="1" cellpadding="1" class="table-0" style="margin-left:4px">
					<colgroup>
	        			<col width=10%></col>
	        			<col width=26%></col>
	        			<col width=10%></col>
	        			<col width=54%></col>
	    			</colgroup>
					<c:forEach var="item" begin="1" end="5" step="1">
					<tr>			   	
				      	<th height="26px">文件名称：</th>
				      	<td>
							<input type="file" style="width:174px" name="fileName${item}" id="fileName${item}" class="TextBox">
				     	</td>
						<th>文件说明：</th>
				      	<td>
				      		<input type="text" maxlength="50" name="mark${item}" id="mark${item}" class="TextBox">
				      		<img class="make-a" src="/images/make-a.gif" onClick="selectAccMark('mark${item}');" style="cursor:pointer;" />
							<select name='extId${item}' id='extId${item}' style="width:80px" onChange="doWcChange(${item});">
								<option value="0">==请选择==</option>
								<c:forEach var="wcItem" items="${wcList}" varStatus="extIdIndex">
									<option value="${wcItem.id}" <c:if test="${accType!='COOPER' && (extId==0 && extIdIndex.index==0 || extId!=0 && wcItem.id==extId)}">selected</c:if>>${wcItem.wcName}</option>
								</c:forEach>
				  			</select>
							<input type="radio" name="accType${item}" id="accType${item}_1" <c:if test="${accType!='COOPER'}">checked="true"</c:if> onClick="changeFileAccType(${item});">自制
							<input type="radio" name="accType${item}" id="accType${item}_2" <c:if test="${accType=='COOPER'}">checked="true"</c:if> onClick="changeFileAccType(${item});">外协
							<input type="radio" name="accType${item}" id="accType${item}_3" onClick="changeFileAccType(${item});">合同
				     	</td>
					</tr>
					</c:forEach>
					<tr>
				      	<th>文件：</th>
						<td colspan="3" id="curFile" name="curFile">&nbsp;</td>
					</tr>
					<tr>
				      	<th>进度：</th>
				      	<td colspan="3">
							<table border="0" id="proTbl" class="table-c">
								<tr>
									<td style="width:84%">
										<table height="20" border="1" class="table-c">
											<tr>
												<td id="proTd" name="proTd" style="width:1px;" bgcolor="#3333cc"></td>
												<td id="remainTd" name="remainTd" bgcolor="#cccccc" style="width:100%">&nbsp;</td>
											</tr>
										</table>
									</td>
									<td id="percentTd" name="percentTd">0%</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
				      	<th>状态：</th>
						<td colspan="3" id="proSpan" name="proSpan">未上传</td>
					</tr>
				</table>
			</fieldset>
			</div>
		</div>
	</div>
	<div class="modal-buttom">
		<input type="button" class="buttom-a" value="开始上传" id="startButton" onClick="doStart();">
		<input type="button" class="buttom-a" value=" 暂 停 " id="pauseButton" onClick="doPause();" disabled="true">
		<input type="button" class="buttom-a" value=" 取 消 " onClick="doWinReturn();" >
		<input type="hidden" id="pause_flag" value="">
	</div>
</div>
</body>
</html>